/* 引入 接口 - 获取字典 */
import { dictList } from '@/service/edu/dict/index'

interface DictData {
  createBy: string
  createTime: string
  cssClass: string
  default: boolean
  dictCode: number
  dictLabel: string
  dictSort: number
  dictType: string
  dictValue: string
  isDefault: string
  listClass: string
  remark: string
  status: string
  updateBy?: any
  updateTime?: any
}

export async function getDict(dictType: string) {
  const res = await dictList(dictType)
  const { code, data = [] } = res
  if (code === 200) {
    return data as DictData[]
  }
}

export async function getDictLabel(dictType: string, dictValue: number | string) {
  const res: any = await getDict(dictType)
  if (res.length > 0) {
    return res.find((item) => item.dictValue === dictValue)?.dictLabel
  }
  return ''
}

export async function getDictValue(params: any, dictLabel: string) {
  const dictList = typeof params === 'string' ? await getDict(params) : params
  if (dictList.length > 0) {
    return dictList.find((item: any) => item.dictLabel === dictLabel)?.dictValue
  }
  return ''
}
